<?php
/**
 * 线上测试脚本 - 测试 Vendor2 消息处理
 * 使用方法：php test_online.php
 */

// 定义应用根目录
define('APP_PATH', __DIR__ . '/application/');
define('RUNTIME_PATH', __DIR__ . '/runtime/');

require __DIR__ . '/thinkphp/base.php';

use think\Db;
use app\service\MqttMessageHandler;
use app\service\MessageParser\MessageParserFactory;

echo "========== 线上 Vendor2 消息处理测试 ==========\n\n";

// 测试数据 - 真实的 Vendor2 消息格式
$topic = 'rziot/zftx_w06/861179035726231/result';
$messageData = [
    "c" => "zidianmao",
    "imei" => "861179035726231",
    "iccid" => "89860325247554801710",
    "ssid" => "MIFI-8915",
    "password" => "1234567890",
    "opname" => "46003",
    "dbm" => "-86",
    "simSwitch" => "2",
    "ip" => "192.168.100.1",
    "lac" => "30555",
    "version" => "JZ12SC_ZIDIANMAO_20251200_V1.0.0",
    "cell_id" => "27235605",
    "speed_downlink" => 0,
    "speed_uplink" => 0,
    "sn" => "86117903572623",
    "boot_time" => "1236643",
    "count" => "8",
    "hidden" => "0"
];

$imei = '861179035726231';

echo "【步骤1】测试消息处理\n";
try {
    $handler = new MqttMessageHandler();
    $result = $handler->handle($topic, $messageData);
    echo $result ? "✅ 消息处理成功\n" : "❌ 消息处理失败\n";
} catch (\Throwable $e) {
    echo "❌ 异常: " . $e->getMessage() . "\n";
    exit(1);
}

echo "\n【步骤2】强制保存到数据库\n";
try {
    $handler->flush();
    echo "✅ flush() 执行完成\n";
} catch (\Throwable $e) {
    echo "❌ 异常: " . $e->getMessage() . "\n";
    exit(1);
}

echo "\n【步骤3】验证数据库\n";
try {
    $record = Db::name('flow_wifi_imei')->where('IMEI', $imei)->find();
    if ($record) {
        echo "✅ 数据库中找到记录\n";
        echo "   IMEI: " . $record['IMEI'] . "\n";
        echo "   MAC: " . $record['mac'] . "\n";
        echo "   ICCID: " . $record['iccid'] . "\n";
        echo "   信号强度: " . $record['signal_strength'] . "\n";
        echo "   更新时间: " . $record['update_time'] . "\n";
    } else {
        echo "❌ 数据库中没有找到记录\n";
        exit(1);
    }
} catch (\Throwable $e) {
    echo "❌ 数据库查询异常: " . $e->getMessage() . "\n";
    exit(1);
}

echo "\n========== ✅ 测试通过 ==========\n";

